package cn.edu.ctbu.dp01.api;

import cn.edu.ctbu.dp01.entity.teacher;
import cn.edu.ctbu.dp01.service.teacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class teacherApiController {

    @Autowired
    private teacherService teacherService;

    // 获取所有教师列表
    @GetMapping("/list")
    public List<teacher> findAll() {
        return teacherService.getAll();
    }

    // 根据 ID 获取单个教师详情
    @GetMapping("/{id}")
    public teacher findById(@PathVariable Integer id) {
        return teacherService.findById(id);
    }

    // 添加教师
    @PostMapping("/add")
    public teacher add(@RequestBody teacher teacher) {
        return teacherService.add(teacher);
    }

    // 更新教师
    @PutMapping("/update")
    public teacher update(@RequestBody teacher teacher) {
        return teacherService.update(teacher);
    }

    // 删除教师
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        teacherService.delete(id);
    }

    @GetMapping("/search")
    public List<teacher> searchByName(@RequestParam String name) {
        return teacherService.findByName(name); // 调用 service 层查询教师
    }
}
